//
//  QHUpdateProgressViewController.m
//  MUGH
//
//  Created by 李志良的mac mini on 2020/5/6.
//  Copyright © 2020 MUGH. All rights reserved.
//

#import "QHUpdateProgressViewController.h"

@interface QHUpdateProgressViewController ()

@end

@implementation QHUpdateProgressViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor clearColor];
    
    [self setUpUI];
}

-(void)setUpUI{
    
    [[UIApplication sharedApplication].keyWindow addSubview:self.contentView];
    
    [self.contentView addSubview:self.maskView];
    [self.contentView addSubview:self.topRoketView];
    
    [self.topRoketView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(SCREEN_WIDTH*0.8);
        make.centerY.equalTo(self.contentView).offset(-95);
        make.height.mas_equalTo(195);
        make.centerX.mas_equalTo(self.contentView);
    }];
    
    UILabel * titleLabel = [[UILabel alloc]init];
    [titleLabel setTextColor:[UIColor whiteColor]];
    [titleLabel setText:@"版本更新"];
    [titleLabel setFont:[UIFont boldSystemFontOfSize:18]];
    [self.topRoketView addSubview:titleLabel];
    
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(200);
        make.top.mas_equalTo(self.topRoketView.mas_top).offset(50);
        make.left.mas_equalTo(self.topRoketView.mas_left).offset(25);
        make.height.mas_equalTo(25);
    }];
    
    
    //边框
    UIView *borderView = [[UIView alloc] init];
    borderView.layer.cornerRadius = 4;
    borderView.layer.masksToBounds = YES;
    borderView.backgroundColor = [UIColor whiteColor];
    borderView.layer.borderColor = RGBA(123, 164, 204, 1).CGColor;
    borderView.layer.borderWidth = 1;
    [self.topRoketView addSubview:borderView];
    
    self.progressBorderView = borderView;
    
    [borderView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.topRoketView.mas_left).offset(25);
        make.right.mas_equalTo(self.topRoketView.mas_right).offset(-25);
        make.bottom.mas_equalTo(self.topRoketView.mas_bottom).offset(-30);
        make.height.mas_equalTo(8);
    }];
    
    //进度
    UIView *tView = [[UIView alloc] init];
    tView.backgroundColor = RGBA(90, 124, 188, 1);
    tView.layer.cornerRadius = 4;
    tView.layer.masksToBounds = YES;
    self.progressView = tView;
    [self.topRoketView addSubview:tView];
    
    [self.progressView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.topRoketView.mas_left).offset(25);
        make.width.mas_equalTo(0);
        make.bottom.mas_equalTo(self.topRoketView.mas_bottom).offset(-30);
        make.height.mas_equalTo(8);
    }];
    
    [self.contentView addSubview:self.bottomContentView];
    
    [self.bottomContentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(SCREEN_WIDTH*0.76);
        make.top.mas_equalTo(self.topRoketView.mas_bottom).offset(-25);
        make.centerX.mas_equalTo(self.topRoketView.mas_centerX);
        make.height.mas_equalTo(75);
    }];
    
    [self.contentView addSubview:self.alertLabel];
    [self.alertLabel setText:@"更新中，请稍候"];
    
    [self.alertLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(SCREEN_WIDTH*0.8);
        make.top.mas_equalTo(self.bottomContentView.mas_top);
        make.left.mas_equalTo(self.topRoketView.mas_left);
        make.height.mas_equalTo(30);
    }];
    
    [self.bottomContentView addSubview:self.opraView];
    [self.opraView setHidden:YES];

    
    [self.opraView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(self.bottomContentView.mas_width);
        make.top.mas_equalTo(self.alertLabel.mas_bottom).offset(5);
        make.bottom.mas_equalTo(self.bottomContentView.mas_bottom);
        make.left.mas_equalTo(self.bottomContentView.mas_left);
    }];
    
    UIView * lineView = [[UIView alloc]init];
    [lineView setBackgroundColor:RGBA(220, 220, 220, 1)];
    [self.opraView addSubview:lineView];
    
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(self.opraView.mas_width);
        make.top.mas_equalTo(self.opraView.mas_top);
        make.left.mas_equalTo(self.opraView.mas_left);
        make.height.mas_equalTo(1);
    }];
    
    UIView * v_Line = [[UIView alloc]init];
    [v_Line setBackgroundColor:RGBA(220, 220, 220, 1)];
    [self.opraView addSubview:v_Line];
    
    [v_Line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(1);
        make.top.mas_equalTo(self.opraView.mas_top);
        make.bottom.mas_equalTo(self.opraView.mas_bottom);
        make.centerX.mas_equalTo(self.opraView.mas_centerX);
    }];
    
    UIButton * cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [cancelButton addTarget:self action:@selector(hideProgress) forControlEvents:UIControlEventTouchUpInside];
    [cancelButton setTitle:@"取消" forState:UIControlStateNormal];
    [cancelButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    [self.opraView addSubview:cancelButton];
    
    [cancelButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.opraView.mas_top);
        make.left.mas_equalTo(self.opraView.mas_left);
        make.right.mas_equalTo(v_Line.mas_left);
        make.bottom.mas_equalTo(self.opraView.mas_bottom);
    }];
    
    UIButton * retryButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [retryButton addTarget:self action:@selector(retryButtonTapped) forControlEvents:UIControlEventTouchUpInside];
    [retryButton setTitle:@"重试" forState:UIControlStateNormal];
    [retryButton setTitleColor:RGBA(90, 124, 188, 1) forState:UIControlStateNormal];
    [self.opraView addSubview:retryButton];
    
    [retryButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.opraView.mas_top);
        make.left.mas_equalTo(v_Line.mas_right);
        make.right.mas_equalTo(self.opraView.mas_right);
        make.bottom.mas_equalTo(self.opraView.mas_bottom);
    }];
}

-(void)show{
    [_contentView setAlpha:1.0];
}

-(void)showErrorMessage{
    [self.alertLabel setTextColor:RGBA(242, 8, 8, 1)];
    [self.alertLabel setText:@"网络错误，下载失败～"];
    [self.opraView setHidden:NO];
}

-(void)showCompleteMessage{
    [self.alertLabel setText:@"更新成功！"];
}

-(void)showNetWorkStatus:(NSInteger)status
{
    if (status == 0)
    {
        [self.alertLabel setTextColor:RGBA(242, 8, 8, 1)];
        [self.alertLabel setText:@"网络错误，请检查网络～"];
        [self.opraView setHidden:YES];
    }
    else
    {
        [self.alertLabel setTextColor:RGBA(242, 8, 8, 1)];
        [self.alertLabel setText:@"Wi-Fi环境下更新更快哦～"];
        [self.opraView setHidden:YES];
    }
}

-(void)hideProgress{
    [_contentView setAlpha:0.0];
    [self dismissViewControllerAnimated:NO completion:nil];
}

-(void)setUpdateProgress:(CGFloat)updateProgress{
    [self.progressView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(updateProgress * self.progressBorderView.frame.size.width);
    }];
}

-(void)retryButtonTapped{
    self.opraView.hidden = YES;
    [self.alertLabel setText:@"更新中，请稍候"];
    [self.alertLabel setTextColor:RGBA(90, 124, 188, 1)];
    if (self.retryBlockTap) {
        self.retryBlockTap();
    }
    
    NSLog(@"=========>>>>>%f",self.progressBorderView.frame.size.width);
    [self.progressView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(0.1*self.progressBorderView.frame.size.width);
    }];
}

-(UIView*)contentView{
    if(!_contentView){
        _contentView = [[UIView alloc]initWithFrame:CGRectMake(0.0, 0.0, Main_Screen_Width, Main_Screen_Height)];
    }
    return _contentView;
}

-(UIView*)maskView{
    if(!_maskView){
        _maskView = [[UIView alloc]initWithFrame:CGRectMake(0.0, 0.0, Main_Screen_Width, Main_Screen_Height)];
        [_maskView setBackgroundColor:RGBA(0, 0, 0, 0.3)];
    }
    return _maskView;
}

-(UIImageView*)topRoketView{
    if(!_topRoketView){
        _topRoketView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"update_progress_rocket"]];
    }
    return _topRoketView;
}

#pragma mark - Properties

-(UIView*)bottomContentView{
    if(!_bottomContentView){
        _bottomContentView = [[UIView alloc]init];
        [_bottomContentView setBackgroundColor:[UIColor whiteColor]];
        [_bottomContentView.layer setCornerRadius:10];
        _bottomContentView.layer.masksToBounds=YES;
        _bottomContentView.clipsToBounds = YES;
    }
    return _bottomContentView;
}

-(UILabel*)alertLabel{
    if(!_alertLabel){
        _alertLabel = [[UILabel alloc]init];
        [_alertLabel setTextAlignment:NSTextAlignmentCenter];
        [_alertLabel setTextColor:RGBA(90, 124, 188, 1)];
        [_alertLabel setFont:[UIFont systemFontOfSize:14]];
    }
    return _alertLabel;
}

-(UIView*)opraView{
    if(!_opraView){
        _opraView = [[UIView alloc]init];
    }
    return _opraView;
}

@end
